/*
 *  This class is part of WorkTimeCalculator Android App
 *  Author: Domenico Pio Novelli
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.dpndeveloper.wtc;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import kankan.wheel.widget.WheelView;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.dpndeveloper.wtc.adapter.WtcIntervalListAdapter;
import com.dpndeveloper.wtc.dataprovider.WtcInterval;
import com.dpndeveloper.wtc.dataprovider.WtcIntervalDataBaseHelper;
import com.dpndeveloper.wtc.dialogs.IntervalDialog;
import com.dpndeveloper.wtc.listener.OnDateChangedListener;
import com.dpndeveloper.wtc.listener.OnIntervalSetEvent;
import com.dpndeveloper.wtc.listener.OnIntervalSetListener;
import com.dpndeveloper.wtc.util.TimeUtil;
import com.dpndeveloper.wtc.util.widget.DateWidget;

/**
 * <p>
 * Day History Activity: activity to show/manage history selecting a day
 * <p>
 * 
 * @author Domenico Pio Novelli
 * 
 */
public class WtcDayHistoryActivity extends Activity implements
		OnDateChangedListener, OnIntervalSetListener {

	/* Constants for dialogs */
	private static final int			ADD_INTERVAL_DIALOG	= 1;

	/* Date Widget to select the day */
	private DateWidget					m_dateWidget;

	/* Instance of DatabaseHelper to manage WtcIntervals */
	private WtcIntervalDataBaseHelper	m_wtcIntervalDatabaseHelper;

	/* The interval of selected day */
	private List<WtcInterval>			m_dayIntervals;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_day_history);

		// Initialize wheels
		WheelView day = (WheelView) findViewById(R.id.historyDay);
		WheelView month = (WheelView) findViewById(R.id.historyMonth);
		WheelView year = (WheelView) findViewById(R.id.historyYear);

		m_dateWidget = new DateWidget(this, day, month, year);
		updateWidgets();
		m_dateWidget.addOnDateChangedListener(this);

		ListView list = (ListView) findViewById(R.id.listViewElement);
		registerForContextMenu(list);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_day_history, menu);
		return true;
	}

	@Override
	public void onDateChanged(DateWidget dateWidget) {
		// update content if it is selected a day
		updateWidgets();
	}

	@Override
	public void onIntervalSet(OnIntervalSetEvent event) {
		// update content if intervals are changed
		updateWidgets();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_day:
			showDialog(ADD_INTERVAL_DIALOG);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.listViewElement) {
			// Context menu for Listview of Intervals
			menu.add(0, 0, 0, R.string.modify);
			menu.add(0, 1, 1, R.string.delete);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		int menuItemIndex = item.getItemId();
		switch (menuItemIndex) {
		case 0:
			// Modify interval: open dialog
			IntervalDialog intervalDialog = new IntervalDialog(this,
					m_dayIntervals.get(info.position), this,
					getWtcIntervalDataBaseHelper(), false);
			intervalDialog.show();
			break;
		case 1:
			// Delete Interval
			Log.i("onContextItemSelected", "Selected Delete for element "
					+ info.position);
			getWtcIntervalDataBaseHelper().delete(
					m_dayIntervals.get(info.position));
			updateWidgets();
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		m_dateWidget.removeOnDateChangedListener(this);
		m_wtcIntervalDatabaseHelper.close();
		super.onDestroy();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ADD_INTERVAL_DIALOG:
			WtcInterval interval = new WtcInterval();
			interval.setStartTime(m_dateWidget.getSelectedDate());
			return new IntervalDialog(this, interval, this,
					m_wtcIntervalDatabaseHelper, false);
		default:
			return super.onCreateDialog(id);
		}
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case ADD_INTERVAL_DIALOG:
			WtcInterval interval = new WtcInterval();
			interval.setStartTime(m_dateWidget.getSelectedDate());
			((IntervalDialog) dialog).setWtcInterval(interval);
			break;
		}
	}

	/**
	 * Update the contents of Activity
	 */
	private void updateWidgets() {

		Calendar startDate = m_dateWidget.getSelectedDate();
		Calendar endDate = Calendar.getInstance(TimeZone.getDefault());
		endDate.setTime(startDate.getTime());
		endDate.set(Calendar.HOUR_OF_DAY, 23);
		endDate.set(Calendar.MINUTE, 59);
		endDate.set(Calendar.SECOND, 59);
		endDate.set(Calendar.MILLISECOND, 999);

		m_dayIntervals = getWtcIntervalDataBaseHelper().selectByInterval(
				startDate, endDate);

		TextView dayInfo = (TextView) findViewById(R.id.textViewDayInfo);
		dayInfo.setText(TimeUtil.getWorkTimeAsString(m_dayIntervals,
				Calendar.getInstance(TimeZone.getDefault())));

		ListView list = (ListView) findViewById(R.id.listViewElement);
		list.setAdapter(new WtcIntervalListAdapter(this, m_dayIntervals));
	}

	/**
	 * Returns the instance of WtcIntervalDataBaseHelper
	 * 
	 * @return instance of WtcIntervalDataBaseHelper
	 */
	private WtcIntervalDataBaseHelper getWtcIntervalDataBaseHelper() {
		if (m_wtcIntervalDatabaseHelper == null) {
			m_wtcIntervalDatabaseHelper = new WtcIntervalDataBaseHelper(this);
		}
		return m_wtcIntervalDatabaseHelper;
	}

}
